package org.example.TankGan.pojo.Bullet;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.awt.*;

/**
 * @author cc
 * @date 2021/4/18 12:33
 * @Description 子弹生成
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Bullet extends Thread {
    public int x;
    public int y;
    private int direction;
    private int speed;
    public boolean isLive=true;

    public Bullet(int x,int y,int direction){
        this.x= x;
        this.y=y;
        this.direction =direction;
    }
    public Bullet(int x,int y,int direction,int speed){
        this.x= x;
        this.y=y;
        this.direction =direction;
        this.speed = speed;
    }


    //子弹线程
    @Override
    public void run() {
        while (true){

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //根据方向进行移动
            switch (direction){
                case 0:
                    y -= speed;
                    break;
                case 1:
                    x +=speed;
                    break;
                case 2:
                    y+=speed;
                    break;
                case 3:
                    x-=speed;
                    break;
            }
//            System.out.println("x的坐标："+x+"y的坐标："+y);
            //子弹超出边界结束进程
            //子弹碰到敌人结束 根据 islive 判断
            if (!(x>0&x<1000&&y>0&&y<750&&isLive)){
                isLive=false;
//                System.out.println("超出坐标");
                break;
            }
        }
    }
}
